home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 303_01.zip / MAIN.C < prev    next >
Text File  |  1993-04-01  |  7KB  |  367 lines

  1. /*
  2.  *    SCCS:    %W%    %G%    %U%
  3.  *    Main routine etc.
  4.  *
  5.  *EMACS_MODES:c
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <a.out.h>
  11. #ifdef    COFF
  12. #include <ldfcn.h>
  13. #else    /*  !COFF  */
  14. #include <sys/var.h>
  15. #endif    /*  !COFF  */
  16. #include "unc.h"
  17.  
  18. #define    LINELNG    70
  19.  
  20. void    inturdat(), intutext(), intudat(), intlsym();
  21. void    ptext(), pdata(), pabs(), pbss(), lscan();
  22.  
  23. ef_fids    mainfile;
  24.  
  25. #ifndef    COFF
  26. int    par_entry, par_round;    /*  68000 parameters  */
  27. #endif    /*  !COFF  */
  28. int    nmods;            /*  Number of modules it looks like  */
  29.  
  30. char    *tfnam = "split";
  31.  
  32. char    lsyms;            /*  Generate local symbols  */
  33. char    verbose;        /*  Tell the world what we are doing  */
  34. char    noabs;            /*  No non-global absolutes  */
  35. int    rel;            /*  File being analysed is relocatable  */
  36. int    lpos;
  37. #ifdef    COFF
  38. char    shlibout;        /*  output values for shlib constants */
  39. #endif    /*  COFF  */
  40.  
  41. symbol    dosymb();
  42. struct    libit    *getfnam();
  43.  
  44. /*
  45.  *    Get hex characters, also allowing for 'k' and 'm'.
  46.  */
  47.  
  48. int    ghex(str)
  49. register  char    *str;
  50. {
  51.     register  int    result = 0;
  52.     register  int    lt;
  53.  
  54.     for  (;;)  {
  55.         lt = *str++;
  56.         switch  (lt)  {
  57.         default:
  58. err:            (void) fprintf(stderr, "Invalid hex digit \'%c\'\n", lt);
  59.             exit(1);
  60.             
  61.         case '\0':
  62.             return    result;
  63.             
  64.         case '0':case '1':case '2':case '3':case '4':
  65.         case '5':case '6':case '7':case '8':case '9':
  66.             result = (result << 4) + lt - '0';
  67.             continue;
  68.             
  69.         case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':
  70.             result = (result << 4) + lt - 'a' + 10;
  71.             continue;
  72.  
  73.         case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':
  74.             result = (result << 4) + lt - 'A' + 10;
  75.             continue;
  76.         
  77.         case 'k':case 'K':
  78.             if  (*str != '\0')
  79.                 goto  err;
  80.             return  result << 10;
  81.             
  82.         case 'm':case 'M':
  83.             if  (*str != '\0')
  84.                 goto  err;
  85.             return  result << 20;
  86.         }
  87.     }
  88. }
  89.  
  90. /*
  91.  *    Process entry line options.  Return number dealt with.
  92.  */
  93.  
  94. int    doopts(av)
  95. char    *av[];
  96. {
  97.     register  int    cnt = 0, lt;
  98.     register  char    *arg;
  99. #ifndef    COFF
  100.     struct    var    vs;
  101. #endif    /*  !COFF  */
  102.     
  103. #ifndef    COFF
  104.     uvar(&vs);
  105.     par_entry = vs.v_ustart;
  106.     par_round = vs.v_txtrnd - 1;
  107.     
  108. #endif    /*  !COFF  */
  109.     for  (;;)  {
  110.         arg = *++av;
  111.         if  (*arg++ != '-')
  112.             return    cnt;
  113.         cnt++;
  114.         
  115. nx:        switch  (lt = *arg++)  {
  116.         default:
  117.             (void) fprintf(stderr, "Bad option -%c\n", lt);
  118.             exit(1);
  119.             
  120.         case  '\0':
  121.             continue;
  122.             
  123.         case  'l':    /*  A file name  */
  124.         case  'L':
  125.             return    cnt - 1;
  126.             
  127.         case  's':
  128.             lsyms++;
  129.             goto  nx;
  130.             
  131.         case  'v':
  132.             verbose++;
  133.             goto  nx;
  134. #ifdef    COFF
  135.  
  136.         case  'V':
  137.             shlibout++;
  138.             goto  nx;
  139.  
  140. #else    /*  !COFF  */
  141.             
  142. #endif    /*  !COFF  */
  143.         case  'a':
  144.             noabs++;
  145.             goto  nx;
  146.  
  147. #ifdef    COFF
  148.         case  't':
  149. #else    /*  !COFF  */
  150.         case  'R':
  151.         case  'N':
  152. #endif    /*  !COFF  */
  153.             if  (*arg == '\0')  {
  154.                 cnt++;
  155.                 arg = *++av;
  156. #ifdef    COFF
  157.                 if  (arg == NULL) {
  158. #else    /*  !COFF  */
  159.                 if  (arg == NULL)  {
  160. #endif    /*  !COFF  */
  161. bo:                    (void) fprintf(stderr,"Bad -%c option\n",lt);
  162.                     exit(1);
  163. #ifdef    COFF
  164.                        }
  165. #else    /*  !COFF  */
  166.                 }
  167. #endif    /*  !COFF  */
  168.             }
  169. #ifndef    COFF
  170.             if  (lt == 'R')
  171.                 par_entry = ghex(arg);
  172.             else
  173.                 par_round = ghex(arg) - 1;
  174.             continue;
  175.             
  176.         case  't':
  177.             if  (*arg == '\0')  {
  178.                 cnt++;
  179.                 arg = *++av;
  180.                 if  (arg == NULL)
  181.                     goto  bo;
  182.             }
  183. #endif    /*  !COFF  */
  184.             tfnam = arg;
  185.             continue;
  186.             
  187.         case  'o':
  188.             if  (*arg == '\0')  {
  189.                 cnt++;
  190.                 arg = *++av;
  191.                 if  (arg == NULL)
  192.                     goto  bo;
  193.             }
  194.             if  (freopen(arg, "w", stdout) == NULL)  {
  195.                 (void) fprintf(stderr, "Help! cant open %s\n", arg);
  196.                 exit(20);
  197.             }
  198.             continue;
  199.         }
  200.     }
  201. }
  202.     
  203. /*
  204.  *    Open binary files.  Arrange to erase them when finished.
  205.  */
  206.  
  207. void    bfopen(nam, fid)
  208. char    *nam;
  209. ef_fid    fid;
  210. {
  211.     char    fnam[80];
  212.     
  213.     (void) sprintf(fnam, "%s.tx", nam);
  214.     if  ((fid->ef_t = open(fnam, O_RDWR|O_CREAT, 0666)) < 0)  {
  215. efil:        (void) fprintf(stderr, "Help could not open %s\n", fnam);
  216.         exit(4);
  217.     }
  218.     (void) unlink(fnam);
  219.     (void) sprintf(fnam, "%s.dt", nam);
  220.     if  ((fid->ef_d = open(fnam, O_RDWR|O_CREAT, 0666)) < 0)
  221.         goto  efil;
  222.     (void) unlink(fnam);
  223. }
  224.  
  225. /*
  226.  *    Close binary files.  They should get zapped anyway.
  227.  */
  228.  
  229. void    bfclose(fid)
  230. ef_fid    fid;
  231. {
  232.     (void) close(fid->ef_t);
  233.     (void) close(fid->ef_d);
  234. }
  235.  
  236. /*
  237.  *    Main routine.
  238.  */
  239.  
  240. main(argc, argv)
  241. int    argc;
  242. char    *argv[];
  243. {
  244.     int    i;
  245.     char    *progname = argv[0];
  246.     char    *msg;
  247.     register  struct  libit  *lfd;
  248.     
  249. #ifdef    COFF
  250.     setbuf(stdout,NULL);
  251.     setbuf(stderr,NULL);
  252.  
  253. #endif    /*  COFF  */
  254.     i = doopts(argv);
  255.     argc -= i;
  256.     argv += i;
  257.     
  258.     if  (argc < 2)  {
  259.         (void) fprintf(stderr, "Usage: %s [ options ] file\n", progname);
  260.         exit(1);
  261.     }
  262.     
  263.     lfd = getfnam(argv[1]);
  264. #ifdef    COFF
  265.     if  (TYPE(lfd->ldptr) == ARTYPE)  {
  266. #else    /*  !COFF  */
  267.     if  (lfd->lf_next > 0)  {
  268. #endif    /*  !COFF  */
  269.         (void) fprintf(stderr, "Main file (%s) cannot be library\n", argv[1]);
  270.         exit(2);
  271.     }
  272.     
  273.     bfopen(tfnam, &mainfile);
  274.     if  (verbose)
  275.         (void) fprintf(stderr, "Scanning text\n");
  276. #ifdef    COFF
  277.     if  (!rtext(lfd->ldptr, &mainfile))  {
  278. #else    /*  !COFF  */
  279.     if  (!rtext(lfd->lf_fd, lfd->lf_offset, &mainfile))  {
  280. #endif    /*  !COFF  */
  281.         msg = "text";
  282. bf:        (void) fprintf(stderr, "Bad format input file - reading %s\n", msg);
  283.         exit(5);
  284.     }
  285.     if  (verbose)
  286.         (void) fprintf(stderr, "Scanning data\n");
  287. #ifdef    COFF
  288.     if  (!rdata(lfd->ldptr, &mainfile))  {
  289. #else    /*  !COFF  */
  290.     if  (!rdata(lfd->lf_fd, lfd->lf_offset, &mainfile))  {
  291. #endif    /*  !COFF  */
  292.         msg = "data";
  293.         goto  bf;
  294.     }
  295.     if  (verbose)
  296.         (void) fprintf(stderr, "Scanning symbols\n");
  297. #ifdef    COFF
  298.     if  (!rsymb(lfd->ldptr, dosymb, &mainfile))  {
  299. #else    /*  !COFF  */
  300.     if  (!rsymb(lfd->lf_fd, lfd->lf_offset, dosymb, &mainfile))  {
  301. #endif    /*  !COFF  */
  302.         msg = "symbols";
  303.         goto  bf;
  304.     }
  305.     if  (verbose)
  306.         (void) fprintf(stderr, "Scanning for relocation\n");
  307. #ifdef    COFF
  308.     if  ((rel = rrel(lfd->ldptr, lfd->ldptr2, &mainfile)) < 0)  {
  309. #else    /*  !COFF  */
  310.     if  ((rel = rrel(lfd->lf_fd, lfd->lf_offset, &mainfile)) < 0)  {
  311. #endif    /*  !COFF  */
  312.         msg = "reloc";
  313.         goto  bf;
  314.     }
  315.     
  316.     if  (rel)  {
  317.         if  (verbose)
  318.             (void) fprintf(stderr, "File is relocatable\n");
  319.         if  (argc > 2)
  320.             (void) fprintf(stderr, "Sorry - no scan on reloc files\n");
  321.     }
  322.     else
  323.         lscan(argc - 2, &argv[2]);
  324.  
  325.     if  (verbose)
  326.         (void) fprintf(stderr, "End of input\n");
  327.     
  328. #ifdef    COFF
  329.     ldaclose(lfd->ldptr2);
  330.     ldclose(lfd->ldptr);
  331. #else    /*  !COFF  */
  332.     (void) close(lfd->lf_fd);
  333. #endif    /*  !COFF  */
  334.     if  (nmods > 0)
  335.         (void) fprintf(stderr, "Warning: at least %d merged modules\n",
  336.             nmods + 1);
  337.  
  338.     if  (mainfile.ef_stvec != NULL)  {
  339.         free(mainfile.ef_stvec);
  340.         mainfile.ef_stvec = NULL;
  341.         mainfile.ef_stcnt = 0;
  342.     }
  343.     
  344.     if  (verbose)
  345.         (void) fprintf(stderr, "Text anal 1\n");
  346.     intutext();
  347.     if  (verbose)
  348.         (void) fprintf(stderr, "Data anal 1\n");
  349.     intudat(&mainfile);
  350.     if  (!rel)  {
  351.         if  (verbose)
  352.             (void) fprintf(stderr, "Data anal 2\n");
  353.         inturdat(&mainfile);
  354.     }
  355.     if  (lsyms)  {
  356.         if  (verbose)
  357.             (void) fprintf(stderr, "Local symbol scan\n");
  358.         intlsym();
  359.     }
  360.     pabs();
  361.     ptext(&mainfile);
  362.     pdata(&mainfile);
  363.     pbss(&mainfile);
  364.     bfclose(&mainfile);
  365.     exit(0);
  366. }
  367.